package com.ruoyi.riskAssessment.service.impl;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.ruoyi.riskAssessment.domain.RiskTrainDept;
import com.ruoyi.riskAssessment.domain.RiskTrainScore;
import com.ruoyi.riskAssessment.dto.RiskTrainScoreDto;
import com.ruoyi.riskAssessment.dto.RiskTrainScoreListDto;
import com.ruoyi.riskAssessment.mapper.RiskTrainDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.riskAssessment.mapper.RiskTrainMapper;
import com.ruoyi.riskAssessment.domain.RiskTrain;
import com.ruoyi.riskAssessment.service.IRiskTrainService;

/**
 * 风险-培训管理Service业务层处理
 *
 * @author lth
 * @date 2023-12-15
 */
@Service
public class RiskTrainServiceImpl implements IRiskTrainService
{
    @Autowired
    private RiskTrainMapper riskTrainMapper;

    @Autowired
    private RiskTrainDeptMapper riskTrainDeptMapper;

    /**
     * 查询风险-培训管理
     *
     * @param id 风险-培训管理主键
     * @return 风险-培训管理
     */
    @Override
    public RiskTrain selectRiskTrainById(Long id)
    {
        return riskTrainMapper.selectRiskTrainById(id);
    }

    /**
     * 查询风险-培训管理列表
     *
     * @param riskTrain 风险-培训管理
     * @return 风险-培训管理
     */
    @Override
    public List<RiskTrain> selectRiskTrainList(RiskTrain riskTrain)
    {
        return riskTrainMapper.selectRiskTrainList(riskTrain);
    }

    /**
     * 新增风险-培训管理
     *
     * @param riskTrain 风险-培训管理
     * @return 结果
     */
    @Override
    public int insertRiskTrain(RiskTrain riskTrain)
    {
        return riskTrainMapper.insertRiskTrain(riskTrain);
    }

    /**
     * 修改风险-培训管理
     *
     * @param riskTrain 风险-培训管理
     * @return 结果
     */
    @Override
    public int updateRiskTrain(RiskTrain riskTrain)
    {
        return riskTrainMapper.updateRiskTrain(riskTrain);
    }

    /**
     * 批量删除风险-培训管理
     *
     * @param ids 需要删除的风险-培训管理主键
     * @return 结果
     */
    @Override
    public int deleteRiskTrainByIds(Long[] ids)
    {
        return riskTrainMapper.deleteRiskTrainByIds(ids);
    }

    /**
     * 删除风险-培训管理信息
     *
     * @param id 风险-培训管理主键
     * @return 结果
     */
    @Override
    public int deleteRiskTrainById(Long id)
    {
        return riskTrainMapper.deleteRiskTrainById(id);
    }

    /**     打分
    * @Author lth
    * @Description //TODO
    * @Date 10:07 2023/12/18
    * @Param
    * @return
     **/
    @Override
    public List<RiskTrainScoreListDto> scoring(Long id) {
        List<RiskTrainScoreDto> riskTrainScoreList = riskTrainMapper.scoring(id);

        Set<Long> trainDeptIds = riskTrainScoreList.stream().map(riskTrainScoreDto -> riskTrainScoreDto.getTrainDeptId()).collect(Collectors.toSet());

        List<RiskTrainScoreListDto> dtoList = trainDeptIds.stream().map(trainDeptId -> {
            RiskTrainScoreListDto riskTrainScoreListDao = new RiskTrainScoreListDto();
            List<RiskTrainScore> scoreList = riskTrainDeptMapper.getInfo(trainDeptId);
            riskTrainScoreListDao.setScoreList(scoreList);
            riskTrainScoreListDao.setTrainId(id);

            RiskTrainDept trainDept = riskTrainDeptMapper.selectRiskTrainDeptById(trainDeptId);
            riskTrainScoreListDao.setCode(trainDept.getCode());
            riskTrainScoreListDao.setName(trainDept.getName());
            riskTrainScoreListDao.setId(trainDeptId);

            return riskTrainScoreListDao;
        }).collect(Collectors.toList());
        return dtoList;
    }
}
